/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.diary;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.account.SysUser;
import com.unis.model.diary.Diary;
import com.unis.service.diary.DiaryService;

/**
 * @author Administrator (email:357375084@qq.com)
 * @since 2013-02-25
 */
@Controller
@RequestMapping("/diary")
public class DiaryController {

    private DiaryService diaryService;

    /****************************************
     * Insert a instace of Diary
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertDiary(@RequestBody Diary diary) throws Exception {
        diary.setStatmentId("Diary.insert");
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser)currentUser.getPrincipal();
        diary.setUserid(user.getUserId());
        String status = "failure";
        diaryService.insert(diary);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Diary
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Diary diary) throws Exception {
        diary.setStatmentId("Diary.update");
        String status = "failure";
        diaryService.update(diary);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Diary
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Diary diary) throws Exception {
        diary.setStatmentId("Diary.delete");
        String status = "failure";
        diaryService.batchDelete(diary);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Diary
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Diary diary = new Diary();
        //暂时用用户名
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser)currentUser.getPrincipal();
        diary.setUserid(user.getUserId());
        diary.setStatmentId("Diary.queryAsList");
        BeanUtils.populate(diary, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(diary, pageMap);
        List<BaseModel> diaryList = diaryService.queryAsList(diary);
        Map<String, Object> renderMap = GridUIMapper.render(diary, diaryList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of Diary
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Diary diary) throws Exception {
        diary.setStatmentId("Diary.queryAsObject");
        return diaryService.queryAsObject(diary);
    }

    @RequestMapping("/page")
    public String forword() {
        return "diary/diaryList";
    }

    @Resource
    public void setDiaryService(DiaryService diaryService) {
        this.diaryService = diaryService;
    }
}
